package com.woniuxy.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.portal.common.enumeration.Assert;
import com.woniuxy.portal.common.enumeration.BusinessCode;
import com.woniuxy.portal.common.enumeration.UserStatus;
import com.woniuxy.portal.model.dto.LoginRequest;
import com.woniuxy.portal.model.entity.User;
import com.woniuxy.portal.service.UserService;
import com.woniuxy.portal.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
* @author scott
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2024-10-18 11:00:44
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseEntity<?> login(LoginRequest param) {

        String key = param.getKey();

        //去redis 获取验证码
        String redisCode = stringRedisTemplate.opsForValue().get(key);
        //校验验证码
        //StringUtils.isEmpty  判空 长度判断
        //StringUtils.hasText  判断内容(更加详细)
//        if (StringUtils.hasText(redisCode) && !param.getCode().equals(redisCode)){
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("验证码不正确");
//        }
        //验证码失败   验证码忽略大小写
        Assert.error(!param.getCode().equalsIgnoreCase(redisCode) , BusinessCode.INVALID_VERIFICATION);

        String account = param.getAccount();
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getAccount,account);
        User user = getOne(queryWrapper);

        //判断用户名是否存在
        if (Objects.isNull(user)){ //
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户名不存在");
        }

        //判断密码是否正确
//        String password = user.getPassword(); //数据库中的密码
//        String loginPwd = param.getPassword();
//        if (!password.equals(loginPwd)){
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户名或者密码不正确");
//        }
        //加密的判断
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //自己输入的
        String loginPwd = param.getPassword();
        //数据库查询的密文
        String password = user.getPassword();

        //加密工具类的判断 第一个参数是: 明文  第二个参数是：密文
        if (!encoder.matches(loginPwd,password)){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户名或者密码不正确");
        }

        //判断是否账户可用
        //未激活 user.getState() == UserStatus.INACTIVE.getKey()
        //锁定  user.getState() == UserStatus.INACTIVE.getKey()
        if (user.getState() == UserStatus.INACTIVE.getKey() ||
                user.getState() == UserStatus.BLOCKED.getKey()){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户锁定或者未激活...");
        }

        //登录成功
        return ResponseEntity.ok(user);
    }

    /**
     *
     * @param email
     * @return  true    为null 未注册
     *          false   以注册
     */
    @Override
    public boolean isEmailExist(String email) {
        //判断邮箱是否已存在
        if (StringUtils.hasText(email)){
            User user = getOne(Wrappers.lambdaQuery(User.class).eq(User::getEmail, email));
            return Objects.isNull(user);
        }
        return false;
    }

    @Override
    public boolean isAccountExist(String account) {
        //判断account是否已存在
        if (StringUtils.hasText(account)){
            User user = getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccount, account));
            return Objects.isNull(user);
        }
        return false;
    }

    @Override
    public boolean isMobileExist(String mobile) {
        //判断mobile是否已存在
        if (StringUtils.hasText(mobile)){
            User user = getOne(Wrappers.lambdaQuery(User.class).eq(User::getMobile, mobile));
            return Objects.isNull(user);
        }
        return false;
    }
}




